package org.ocean.online.web.workspace;

import java.util.List;
import java.util.Map;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.JavaScriptResourceReference;
import org.hibernate.exception.GenericJDBCException;
import org.ocean.hibernate.CreateTableUtil;
import org.ocean.online.web.workspace.inner.DesignModelService;
import org.ocean.online.wicket.NeedLoginPage;
import org.ocean.online.wicket.SimpleAjaxSubmitLink;
import org.springframework.jdbc.UncategorizedSQLException;

public class DesignModel extends NeedLoginPage {

	Form<?> form = new Form("ajaxForm");
	private HiddenField<String> projectName =new HiddenField<String>("projectName",new Model<String>(""));
	private ListView<Map> modelList;
	private HiddenField<String> modelName =new HiddenField<String>("modelName",new Model<String>(""));
	private HiddenField<String> newModelName =new HiddenField<String>("newModelName",new Model<String>(""));
	HiddenField<String> modelData = new HiddenField<String>("modelData",new Model<String>(""));
	WebMarkupContainer modelMinPanel = new WebMarkupContainer("modelMinPanel");
	private DesignModelService service;
	public DesignModel(){
		init();
		service = new DesignModelService();
		SimpleAjaxSubmitLink link = new SimpleAjaxSubmitLink("alink"){

			@Override
			protected void onSubmit(AjaxRequestTarget target) {
				// 表元数据
				String data = modelData.getDefaultModelObjectAsString();
				if(data==null || data.equals("")){
					target.appendJavaScript("alert('请至少添加一个字段')");
					return;
				}
				try{
					//需做校验
					service.saveTable(getDb(),projectName.getDefaultModelObjectAsString(),modelName.getDefaultModelObjectAsString(),data);
					modelList.setList(service.queryModels(getDb(),projectName.getDefaultModelObjectAsString()));
					target.add(modelMinPanel);
					target.appendJavaScript("doMenu()");
					target.appendJavaScript("alert('save model success')");
					
				}catch(GenericJDBCException ex){
					target.appendJavaScript("alert('save model failed "+ex.getSQLException().getMessage()+"')" );
				}
				catch(UncategorizedSQLException ex){
					target.appendJavaScript("alert('"+ex.getMessage()+"')");
				}
				catch(Exception ex){
					target.appendJavaScript("alert('save model failed"+ex.getMessage()+"')");
					log.error("",ex);
				}
			}
			
		};
		form.add(link);
		
		List<Map> list = service.queryModels(getDb(),projectName.getDefaultModelObjectAsString());
		modelList = new ListView<Map>("modelList",list) {

			@Override
			protected void populateItem(ListItem<Map> item) {
				Label label = new Label("model",item.getModelObject().get("modelName").toString());
				item.add(label);
			}
		};
		modelMinPanel.add(modelList);
		
		SimpleAjaxSubmitLink deleteModel = new SimpleAjaxSubmitLink("deleteModel"){

			@Override
			protected void onSubmit(AjaxRequestTarget target) {
				service.deleteModel(getDb(),modelName.getDefaultModelObjectAsString());
				modelList.setList(service.queryModels(getDb(),projectName.getDefaultModelObjectAsString()));
				target.add(modelMinPanel);
				target.appendJavaScript("doMenu()");
			}
		};
		form.add(deleteModel);
		
		SimpleAjaxSubmitLink toSql = new SimpleAjaxSubmitLink("toSql"){

			@Override
			public void onSubmit(AjaxRequestTarget target) {
				String sql =CreateTableUtil.getCreateTableSql(modelName.getDefaultModelObjectAsString(),
						service.queryModel(getDb(),modelName.getDefaultModelObjectAsString()));
				target.appendJavaScript("alert('"+sql+"');");
			}
			
		};
		form.add(toSql);
		
		SimpleAjaxSubmitLink rename = new SimpleAjaxSubmitLink("rename"){
			@Override
			public void onSubmit(AjaxRequestTarget target) {
				service.rename(getDb(),projectName.getDefaultModelObjectAsString(),
						modelName.getDefaultModelObjectAsString(),newModelName.getDefaultModelObjectAsString());
			}
		};
		form.add(rename);
	}

	@Override
	public void renderHead(IHeaderResponse response) {
		super.renderHead(response);
		response.render(JavaScriptHeaderItem.forReference(new JavaScriptResourceReference(this.getClass(), "model.js")));
		response.render(JavaScriptHeaderItem.forReference(new JavaScriptResourceReference(this.getClass(), "savemodel.js")));
	}
	
	private void init(){
		add(form);
		projectName.setDefaultModelObject(getStringParam("projectName"));
		newModelName.setDefaultModelObject(getStringParam("newModelName"));
		modelData.setDefaultModelObject(getStringParam("modelData"));
		modelName.setDefaultModelObject(getStringParam("modelName"));
		form.add(newModelName);
		form.add(modelData);
		form.add(modelName);
		form.add(projectName);
		modelMinPanel.setOutputMarkupId(true);
		add(modelMinPanel);
	}
}
